home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / sethostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  836 b   |  43 lines

  1. /*
  2.  *    sethostname - set current name of machine
  3.  */
  4. #include     "lib.h"
  5. #include    <sys/file.h>
  6. #include    <sys/param.h>
  7. #include    <errno.h>
  8.  
  9. #ifndef    MAXHOSTNAMELEN
  10. #  define    MAXHOSTNAMELEN    64        /* just like BSD */
  11. #endif
  12.  
  13. #define min( a, b )  (( a < b ) ? a : b )
  14.  
  15. extern int    errno;
  16. /* static char    namebuf[ MAXHOSTNAMELEN ]; */
  17.  
  18. int sethostname( name, namelen )
  19.   char    *name;
  20.   int    namelen;
  21. {
  22.   int        fd;
  23.  
  24.   if ( getuid() != 0 ) {    /* must be superuser to do this */
  25.     errno = EPERM;
  26.     return( -1 );
  27.   }
  28.   if (creat( HOSTNAME, 0644 ) < 0)    /* (re)create /etc/localhostname */
  29.     return( -1 );
  30.   if ((fd = open( HOSTNAME, O_WRONLY )) < 0) {
  31.     errno = EIO;
  32.     return( -1 );
  33.   }
  34.  
  35.   name[ min( namelen, MAXHOSTNAMELEN ) ] = '\n';
  36.   if (write( fd, name, min( namelen, MAXHOSTNAMELEN )) < 0) {
  37.     errno = EIO;
  38.     return( -1 );
  39.   }
  40.   close( fd );
  41.   return( 0 );
  42. }
  43.